package com.endpoint.book.listener;

import com.endpoint.book.service.BookService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @Project: endpoint
 * @Name: BookVisitAddListener
 * @Author: Jack_Li
 * @Date: 2024/7/8 19:16
 */
@Component
@Slf4j
public class BookVisitAddListener {

    @Autowired
    private BookService bookService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;


    /**
     * 更新数据库
     * 流量削峰，每本小说累积10个点击更新一次
     */
    @SneakyThrows
    @RabbitListener(queues = {"UPDATE-DB-QUEUE"})
    public void updateDb(Long bookId, Channel channel,  Message message) {

        log.debug("收到更新数据库消息：" + bookId);
        RLock lock = redissonClient.getLock("addVisitCountToDb");
        lock.lock();
        try {

            Integer visitCount = (Integer)redisTemplate.opsForValue().get("bookAddVisitCount:" + bookId);
            if (visitCount == null) {//未存储默认为0
                visitCount = 0;
            }
            redisTemplate.opsForValue().set("bookAddVisitCount:" + bookId, ++visitCount);
            if (visitCount >= 100) {

                //数据库添加
                bookService.addVisitCount(bookId, visitCount);
                redisTemplate.delete("bookAddVisitCount:" + bookId);
            }
        }catch (Exception e){
            log.error("更新数据库失败"+bookId);
        }
        lock.unlock();
        Thread.sleep(1000 * 2);
    }

}
